#!/bin/bash
function srv_optimize() {
    echo "服务优化" >>$logs_file
    disable_srv=
    while [ -z "$disable_srv" ]
    do
        disable_srv=$(whiptail --backtitle "Author: Elvis" --title "${serviceSel[0]}" --checklist \
        "${serviceSel[1]}" 12 60 2 \
        "Firewalld" "        ${serviceSel[2]}      " ON \
        "Mail" "        ${serviceSel[3]}          " OFF 3>&1 1>&2 2>&3)
        if [ "$?" != "0" ];then
          sleep 0.10
          return 1
        fi
        if [ -z "$disable_srv" ];then
          whiptail --backtitle "Author: Elvis" --title "$prompt_message" --msgbox "${servicePrompt[0]}！！！" 10 70
        fi
    done

    if [ "$system_ver" == "6" ];then
        disable_srv_rel=${disable_srv//Firewalld/iptables}
        disable_srv_rel=${disable_srv_rel//Mail/sendmail}
    else
        disable_srv_rel=${disable_srv//Firewalld/firewalld}
        disable_srv_rel=${disable_srv_rel//Mail/postfix}
    fi
    n=0
    dis_srv_arr=($disable_srv_rel)
    dis_srv_arr_n=${#dis_srv_arr[*]}
    numb=`expr $dis_srv_arr_n + 1`
    Percent_inc=`expr 100 / $numb`
    Percent=0
    {
    echo 1
    sleep 1
    for i in $disable_srv
    do
      Percent=`expr $Percent + $Percent_inc`
      dis_srv=${dis_srv_arr[$n]}
      msg="${servicePrompt[1]}$dis_srv"
      echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
      echo -e "XXX\n$Percent\n$msg\nXXX"
      echo $Percent
      sleep 1
      if [ "$i" == '"Firewalld"' ];then
         service iptables stop >/dev/null 2>&1 || systemctl stop firewalld >/dev/null 2>&1
         chkconfig --level 2345 iptables off >/dev/null 2>&1 || systemctl disable firewalld >/dev/null 2>&1
      elif [ "$i" == '"sendmail"' ];then
         service sendmail stop >/dev/null 2>&1 || systemctl stop postfix >/dev/null 2>&1
         chkconfig --level 2345 sendmail off >/dev/null 2>&1 || systemctl disable postfix >/dev/null 2>&1
      fi
      let n+=1
    done
    if [[ "$Percent" -lt 100 ]];then
        echo 100
        sleep 1
    fi
    } | whiptail --backtitle "Author: Elvis" --title "${servicePrompt[2]}" --gauge " " 6 60 0 && \
    whiptail --backtitle "Author: Elvis" --title "提示" --msgbox "${servicePrompt[3]}${disable_srv_rel//\"/} ！！！" 10 70
    
    sleep 0.10
    echo "">>$logs_file
    echo "">>$logs_file
}